/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sys.donto.consulta;

import java.util.List;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import sys.donto.util.GenericDAO;
import sys.donto.util.HibernateUtil;

/**
 *
 * @author JOABB
 */
public class ConsultaDAO extends GenericDAO<Consulta>{
     public ConsultaDAO() {
        super(Consulta.class);
    }
    public List <Consulta> pesquisaDescricao(String Nome){
        setSessao(HibernateUtil.getSessionFactory().openSession());
        setTransacao(getSessao().beginTransaction());
        
        List <Consulta> consultas = (List<Consulta>) getSessao().createCriteria(Consulta.class).
                add(Restrictions.ilike("consulta", Nome, MatchMode.ANYWHERE)).
                addOrder(Order.asc("data")).list();
        
        getSessao().close();
        return consultas;
    }
    
    public Consulta pesquisaId(int id){
        setSessao(HibernateUtil.getSessionFactory().openSession());
        setTransacao(getSessao().beginTransaction());
        
        Consulta consulta = (Consulta) getSessao().createCriteria(Consulta.class).
                add(Restrictions.eq("id", id)).uniqueResult();
        
        getSessao().close();
        return consulta;
    }
    
    public void salvar(Consulta cons){
        if(cons.getId()==0){
            adicionar(cons);
        } else {
            atualizar(cons);
        }
    }
    
    
}
